package kxfang.com.find.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentPagerAdapter;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.google.android.material.tabs.TabLayout;

import java.util.List;

import kxfang.com.base.fragment.MvvmLazyFragment;
import kxfang.com.base.livedatabus.LiveDatabus;
import kxfang.com.base.utils.GsonUtils;
import kxfang.com.base.viewmodel.IMvvmBaseViewModel;
import kxfang.com.base.viewmodel.MvmBaseViewModel;
import kxfang.com.common.bean.LifeClassModel;
import kxfang.com.common.config.HttpConfig;
import kxfang.com.common.parameter.StorePar;
import kxfang.com.common.router.RoutHub;
import kxfang.com.common.utils.StatusBarUtil;
import kxfang.com.find.R;
import kxfang.com.find.adapter.EnViewPagerAdapter;
import kxfang.com.find.databinding.FindFragmentPersonalInformationBinding;
import kxfang.com.http.EasyHttp;
import kxfang.com.http.callback.SimpleCallBack;
import kxfang.com.http.exception.ApiException;

@Route(path = RoutHub.PAGER_FIND)
public class PersonalInformationFragment extends MvvmLazyFragment<FindFragmentPersonalInformationBinding, IMvvmBaseViewModel> {

    String[] title = {"最新发布", "二手车", "闲置品", "商铺/写字楼", "求购需求"};
    EnViewPagerAdapter adapter;
    private int type1 = 0, type2 = 0, type3 = 0, type4 = 0;
    private int postion=0;
    private long beforeTime = 0;// 防止多次点击

    @Override
    protected void onFragmentFirstVisible() {
        super.onFragmentFirstVisible();
        StatusBarUtil.setTopFragmentView(this, viewDataBinding.fbView,R.color.common_colorWhite);
        getLifeClass();
    }

    private void getLifeClass() {
        //分类接口
        StorePar par = new StorePar();
        par.setCtype(5);
        EasyHttp.post(HttpConfig.getLifeClass)
                .cacheKey(HttpConfig.getLifeClass)
                .upJson(GsonUtils.toJson(par))
                .execute(new SimpleCallBack<List<LifeClassModel>>() {
                    @Override
                    public void onSuccess(List<LifeClassModel> lifeClassModel) {
                        if (lifeClassModel != null && lifeClassModel.size() != 0){
                            type1 = lifeClassModel.get(3).getID();
                            type2 = lifeClassModel.get(2).getID();
                            type3 = lifeClassModel.get(1).getID();
                            type4 = lifeClassModel.get(0).getID();
                            initView();
                        }
                    }

                    @Override
                    public void onError(ApiException e) {

                    }

                    @Override
                    public void onCompleted() {

                    }
                });

    }
    private void initView(){
        adapter = new EnViewPagerAdapter(this.getChildFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        adapter.addFragment(FxFragment.newInstance(0), title[0]);
        adapter.addFragment(FxFragment.newInstance(type1), title[1]);
        adapter.addFragment(FxFragment.newInstance(type2), title[2]);
        adapter.addFragment(FxFragment.newInstance(type3), title[3]);
        adapter.addFragment(FxFragment.newInstance(type4), title[4]);
        viewDataBinding.viewPager.setAdapter(adapter);

        viewDataBinding.tabLayoutId.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                if (null == view) {
                    tab.setCustomView(R.layout.find_tab_layout_text);
                }
                TextView textView = tab.getCustomView().findViewById(android.R.id.text1);
                textView.setTextAppearance(getActivity(), R.style.find_tabLayoutTextSize_four);
                textView.setTextColor(getResources().getColor(R.color.common_color_A9A9A9));
                textView.setTextColor(getResources().getColor(R.color.common_color_333333));
                postion = tab.getPosition();

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                if (null == view) {
                    tab.setCustomView(R.layout.find_tab_layout_text);
                }
                TextView textView = tab.getCustomView().findViewById(android.R.id.text1);
                textView.setTextAppearance(getActivity(), R.style.find_tabLayoutTextSize_three);
                textView.setTextColor(getResources().getColor(R.color.common_color_A9A9A9));
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                int selectedPosition = tab.getPosition();
                if (selectedPosition==postion){
                    long currentTime = System.currentTimeMillis();
                    if (currentTime - beforeTime < 1000) {
//                        EventBus.getDefault().post(new ReturnPayResult(status));
                        LiveDatabus.getInstance().with("双击666").postValue("aa");
                    }
                    beforeTime = currentTime;
                }
            }
        });
        viewDataBinding.tabLayoutId.setupWithViewPager(viewDataBinding.viewPager);
    }

    @Override
    public int getLayoutId() {
        return R.layout.find_fragment_personal_information;
    }

    @Override
    public int getBindingVariable() {
        return 0;
    }

    @Override
    protected IMvvmBaseViewModel getViewModel() {
        return null;
    }

    @Override
    protected void onRetryBtnClick() {

    }

}
